/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.braincasedesigns.roller.model;

import com.braincasedesigns.roller.model.DieRoll;
import com.braincasedesigns.roller.model.DieRollResult;
import com.braincasedesigns.roller.model.Face;
import java.util.Collections;
import java.util.List;
import java.util.ListIterator;

/**
 *
 * @author jcampbel
 */
public class GenericDieRollResult implements DieRollResult<DieRoll,Face> {

    private final DieRoll dieRoll;
    private final List<Face> faces;
    private final int total;
    
    
    public GenericDieRollResult(DieRoll dieRoll,List<Face> faces){
        this.dieRoll = dieRoll;
        this.faces = Collections.unmodifiableList(faces);
        int runningTotal = 0;
        for (Face f: faces){
            runningTotal += f.getNumericRepresentation();
        }
        if (dieRoll.getDieModifier() == null){
            total = runningTotal;
        }
        else {
            total = dieRoll.getDieModifier().apply(runningTotal);
        }
    }
    
    public int getTotal() {
        return total;
    }

    public boolean isModified() {
        return dieRoll.getDieModifier() != null;
    }

    public DieRoll getDieRoll() {
        return dieRoll;
    }

    public String getStringTotal() {
        StringBuilder buf = new StringBuilder();
        buf.append("[");
        ListIterator<Face> fIter = faces.listIterator();
        while (fIter.hasNext()){
            Face face = fIter.next();
            buf.append(face.getStringRepresentation());
            if (fIter.hasNext()){
                buf.append(",");
            }
        }
        buf.append("]");
        if (isModified()){
            buf.append(dieRoll.getDieModifier().getNotation());
        }
        buf.append("=");
        buf.append(total);
        
        return buf.toString();
    }

    public List<Face> getFaces() {
        return faces;
    }

    public int getModifierTotal() {
        return isModified() ? total - dieRoll.getDieModifier().getOperand() : total;
    }

}
